package com.by.controller;

import com.by.bean.ResultBean;
import com.by.bean.ShopCart;
import com.by.bean.ShopCartItem;
import com.by.bean.CartInfo;
import com.by.service.ShopCartItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author wyh
 * @since 2025-04-14
 */
@Api(tags = "购物车接口")
@RestController
@RequestMapping("/api/cart")
public class ShopCartItemController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ShopCartItemController.class);

    @Autowired
    private ShopCartItemService shopCartItemService;

    @ApiOperation(value = "添加商品到购物车")
    @PostMapping("/add")
    public ResultBean addToCart(@RequestBody ShopCartItem shopCartItem) {
        int result = shopCartItemService.addToCart(shopCartItem);
        return saveResult(result);
    }

    @ApiOperation(value = "获取购物车信息")
    @GetMapping("/info")
    public ResultBean getCartInfo(@RequestParam(value = "userId", required = true) String userIdStr) {
        if (StringUtils.isEmpty(userIdStr)) {
            return fail("用户ID不能为空");
        }
        
        try {
            Long userId = Long.parseLong(userIdStr);
            if (userId <= 0) {
                return fail("用户ID必须大于0");
            }
            
            CartInfo cartInfo = shopCartItemService.getCartInfo(userId);
            return success("购物车信息", cartInfo);
        } catch (NumberFormatException e) {
            log.error("用户ID格式错误: {}", userIdStr, e);
            return fail("用户ID格式错误");
        } catch (Exception e) {
            log.error("获取购物车信息失败", e);
            return fail("获取购物车信息失败");
        }
    }

    @ApiOperation(value = "删除购物车商品")
    @DeleteMapping("/delete")
    public ResultBean deleteItem(@RequestParam("userId") Long userId, 
                               @RequestParam("skuId") Long skuId) {
        int result = shopCartItemService.deleteItem(userId, skuId);
        return deleteResult(result);
    }

    @ApiOperation(value = "更新购物车商品数量")
    @GetMapping("/updateCount")
    public ResultBean<Integer> updateCartCount(
            @RequestParam("userId") Long userId,
            @RequestParam("skuId") Long skuId,
            @RequestParam("delta") int delta) {
        log.info("更新购物车商品数量 - 用户ID: {}, SKU ID: {}, 变化量: {}", userId, skuId, delta);
        
        try {
            if (userId == null || skuId == null) {
                return fail("参数不能为空");
            }
            
            if (delta != 1 && delta != -1) {
                return fail("无效的数量变化值");
            }
            
            int result = shopCartItemService.updateCartCount(userId, skuId, delta);
            if (result > 0) {
                return success("更新购物车商品数量成功",result);
            } else {
                return fail("更新购物车商品数量失败");
            }
        } catch (Exception e) {
            log.error("更新购物车商品数量异常", e);
            return fail("更新购物车商品数量失败");
        }
    }

    @ApiOperation(value = "清空购物车")
    @DeleteMapping("/clear")
    public ResultBean clearCart(@RequestParam("userId") Long userId) {
        int result = shopCartItemService.clearCart(userId);
        return deleteResult(result);
    }

    @ApiOperation(value = "更新商品选中状态")
    @PostMapping("/check")
    public ResultBean checkItem(@RequestParam("userId") Long userId,
                              @RequestParam("skuId") Long skuId,
                              @RequestParam("checked") Boolean checked) {
        shopCartItemService.checkItem(userId, skuId, checked);
        return success("更新成功");
    }

    @ApiOperation(value = "获取购物车商品总数")
    @GetMapping("/count")
    public ResultBean getCartCount(@RequestParam("userId") Long userId) {
        Integer count = shopCartItemService.getCartCount(userId);
        return success("购物车商品总数", count);
    }

}
